package com.tsingtree.modular.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanDeviceCate;
import com.tsingtree.modular.entity.NanSeller;
import com.tsingtree.modular.entity.NanUserDevice;
import com.tsingtree.modular.mapper.NanUserDeviceMapper;
import com.tsingtree.modular.model.params.NanUserDeviceParam;
import com.tsingtree.modular.model.result.NanUserDeviceResult;
import com.tsingtree.modular.service.NanDeviceCateService;
import com.tsingtree.modular.service.NanSellerService;
import com.tsingtree.modular.service.NanUserDeviceService;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 用户设备 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2023-08-29
 */
@Service
public class NanUserDeviceServiceImpl extends ServiceImpl<NanUserDeviceMapper, NanUserDevice> implements NanUserDeviceService {

    @Autowired
    private NanSellerService nanSellerService;
    @Autowired
    private NanDeviceCateService nanDeviceCateService;

    @Override
    public void add(NanUserDeviceParam param){
        NanUserDevice entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanUserDeviceParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanUserDeviceParam param){
        NanUserDevice oldEntity = getOldEntity(param);
        NanUserDevice newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanUserDeviceResult findBySpec(NanUserDeviceParam param){
        return null;
    }

    @Override
    public List<NanUserDeviceResult> findListBySpec(NanUserDeviceParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanUserDeviceParam param){
        Page<NanUserDeviceResult> pageContext = getPageContext();
        IPage<NanUserDeviceResult> page = this.baseMapper.customPageList(pageContext, param);
        Map<Integer, String> map = nanSellerService.list().stream().collect(Collectors.toMap(NanSeller::getId, NanSeller::getName));
        Map<Integer, String> cateMap = nanDeviceCateService.list().stream().collect(Collectors.toMap(NanDeviceCate::getId, NanDeviceCate::getName));
        page.getRecords().forEach(obj -> {
            obj.setSellerName(map.get(obj.getSellerId()));
            obj.setCate(cateMap.get(obj.getCateId()));
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanUserDeviceParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanUserDevice getOldEntity(NanUserDeviceParam param) {
        return this.getById(getKey(param));
    }

    private NanUserDevice getEntity(NanUserDeviceParam param) {
        NanUserDevice entity = new NanUserDevice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
